package com.zsh.rabbitmq.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.zsh.rabbitmq.utils.RabbitMQUtils;

import java.util.Scanner;

/**
 * @author zsh
 * @Description 消息在手动应答时不会丢失，放回队列重新消费
 * @create 2022-04-26 17:15
 */
public class Task02 {
    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        //开启发布确认模式
        channel.confirmSelect();
        channel.queueDeclare(TASK_QUEUE_NAME,true,false,false,null);

        //从控制台输入
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String str = scanner.next();

            //设置发送者消息为持久化消息，保存在磁盘上。
            channel.basicPublish("",TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN,str.getBytes("UTF-8"));
            System.out.println("生产者发出消息：" + str);
        }
    }

}
